define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'spider/keywords/index' + location.search,
                    add_url: 'spider/keywords/add',
                    edit_url: 'spider/keywords/edit',
                    del_url: 'spider/keywords/del',
                    multi_url: 'spider/keywords/multi',
                    import_url: 'spider/keywords/import',
                    spider_url: 'spider/keywords/spider',
                    table: 'keywords',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'platform.name', title: __('Spider_platform_id'), operate: 'LIKE', formatter: Table.api.formatter.label},
                        // {field: 'spider_platform_id', title: __('Spider_platform_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [
                                {
                                    name: 'spider',
                                    text: __('抓取'),
                                    title: __('抓取'),
                                    classname: 'btn btn-xs btn-primary btn-magic btn-ajax',
                                    icon: 'fa fa-list',
                                    extend: "data-area='[\"100%\",\"100%\"]'",
                                    url: $.fn.bootstrapTable.defaults.extend.spider_url,
                                    confirm: '确认抓取?',
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


            $('.btn-spider').click(function () {
                var ids =  Table.api.selectedids(table); //取出来是个数组  示例：[1,2,3,4,5]
                ids = ids.join(','); //用逗号隔开  数组分割后：1,2,3,4,5
                layer.confirm('确认进行【批量抓取】操作吗？', {
                    btn: ['确定', '取消'],
                },function (index) {
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.spider_url,
                        data: {ids: ids},
                        loading: true
                    });
                    Layer.close(index);    //关闭弹窗
                });
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
